<?php

namespace app\admin\controller\depot\async;

use app\common\controller\Backend;

/**
 * 商品异步管理
 */
class Product extends Backend
{
    protected $model = null;
    protected $relationSearch = true;
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
      parent::_initialize();
      $this->model = model('Product.Product');
    }

    /**
     * 商品列表
     */
    public function index()
    {
      $this->request->filter(['strip_tags']);
      if ($this->request->isAjax())
      {
        if ($this->request->request('keyField'))
        {
            return $this->selectpage();
        }
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();

        //查询非下架的商品
        $DataWhere = ['status'=>'1'];
        
        $total = $this->model
          ->with(['type','unit'])
          ->where($where)
          ->where($DataWhere)
          ->order($sort, $order)
          ->count();

        $list = $this->model
          ->with(['type','unit'])
          ->where($where)
          ->where($DataWhere)
          ->order($sort, $order)
          ->limit($offset, $limit)
          ->select();


        $result = array("total" => $total, "rows" => $list);

        return json($result);
      }
      return $this->view->fetch();
    }

    /**
     * 返回当前选中商品
     */
    public function choice($ids = NULL)
    {
      $row = $this->model->find(['id' => $ids]);

      if(!$row)
      {
        $this->error(__('No Results were found'));
      }else
      {
        $this->success(null, null, $row);
      }
    }
}
